// pages/teacherKaoqinInfor/teacherKaoqinInfor.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date1: '',
    date2: '',
    rangeKey1: 'campus_name',
    rangeKey2: 'project_name',
    value1: '校区',
    value2: '项目',
    kaoqinList: [],
    kw:''
  },


  //获取校区、项目、班级
  getSchool(campus_id='') {
    api.fetchApi('principal/getbasic', { token: wx.getStorageSync('token'),campus_id }).then(res => {
      var campusArr = res.campus;
      // var classArr = res.class;
      var projectArr = campus_id ? res.project : [];
      //console.log(campusArr, classArr, projectArr)
      this.setData({
        campusArr,
        // classArr,
        projectArr
      })
    })
  },

  //选择学校
  selectSchool(e) {
    var campusArr = this.data.campusArr;
    var rangeKey1 = this.data.rangeKey1;
    var nowArr = campusArr[e.detail]
    console.log(nowArr)
    this.setData({
      value1: nowArr[rangeKey1],
      campus: nowArr.id,
      value2:'项目'
    })

    this.setData({
      kaoqinList: []
    })
    // this.getKaoqinInfor(1)
    this.getSchool(nowArr.id);
  },

  //选择项目
  selectClass(e) {
    var projectArr = this.data.projectArr;
    var rangeKey2 = this.data.rangeKey2;
    var nowArr = projectArr[e.detail]
    console.log(nowArr)
    this.setData({
      value2: nowArr[rangeKey2],
      project_id: nowArr.id
    })
    this.setData({
      kaoqinList: []
    })
    this.getKaoqinInfor(1,this.data.kw)

  },

  //获取考勤信息
  getKaoqinInfor(page,search='') {
    // this.setData({
    //   kaoqinList:[]
    // })
    var params = {
      token: wx.getStorageSync('token'),
      campus_id: this.data.campus,
      project_id: this.data.project_id,
      time: this.data.date1 + '~' + this.data.date2,
      page,
      search
    }
    api.fetchApi('principal/teacher_attendance', params).then(res => {
      var kaoqinList = this.data.kaoqinList,
        kaoqinList1 = res.list,
        conut1 = res.count;
      console.log(kaoqinList1)
      if (kaoqinList1.length == 0 && page > 1) {
        //api.showToast('到底啦!', 'http://qnwmfx.laigl.com/iconimg/warn.png', 1500)
        this.setData({
          isMore: true
        })
        return
      }
      kaoqinList = [...kaoqinList, ...kaoqinList1]
      this.setData({
        kaoqinList,
        page,
        conut1
      })
    })
  },

  //教师搜索
  searchShcool(e){
    var kw=e.detail.value;
    this.setData({
      kw
    })
    this.getKaoqinInfor(1,kw);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var date1 = new Date().dateFormat('Y-m-d');
    var date2 = (new Date(new Date().valueOf() + 86400000)).dateFormat('Y-m-d')
    console.log(date1, date2)
    this.setData({
      date1,
      date2
    })

    this.getSchool()
   // this.getKaoqinInfor(1,this.data.kw);


  },
  //时间始   子组件传过来的事件
  dateChange1(e) {

    this.setData({
      date1: e.detail
    })
    this.setData({
      kaoqinList: []
    })
    this.getKaoqinInfor(1,this.data.kw)
  },

  //时间止
  dateChange2(e) {

    this.setData({
      date2: e.detail
    })
    this.setData({
      kaoqinList: []
    })

    this.getKaoqinInfor(1,this.data.kw)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = this.data.page || 1;
    page++;

    this.getKaoqinInfor(page,this.data.kw)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})